package mysql

import (
	"database/sql"
	"fmt"
	"log"
	"time"
)

var (
	ip     = "127.0.0.12.9"
	port   = "3306"
	user   = "root"
	pwd    = "123456"
	dbname = "course"
)

var DB *sql.DB

func InitDB() error {
	dataSource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=Local", user, pwd, ip, port, dbname)
	var err error
	DB, err = sql.Open("mysql", dataSource)
	if err != nil {
		log.Println(err)
		return err
	}
	DB.SetConnMaxLifetime(time.Minute * 3)
	DB.SetMaxOpenConns(10)
	DB.SetMaxIdleConns(10)
	err = DB.Ping()
	if err != nil {
		log.Println(err)
		return err
	}
	return nil
}
